﻿package com.wandian.view.mediator
{
	import com.greensock.TweenMax;
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.data.GameData;
	import com.wandian.model.InRoomPlayerDataProxy;
	import com.wandian.model.vo.PlayerVo;
	import com.wandian.view.components.ui.CardManage;
	import com.wandian.view.components.ui.SoundManage;

	import flash.display.MovieClip;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;

	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.*;
	/**   
	* @author long
	* @QQ 108232706
	* @version 1.0   
	*/   
	public class DealCardMediator extends Mediator
	{
		/**
		 * 桌上9组背面牌
		 * @default
		 */
		private var arrDeal:Array;
		/**
		 * 桌面上9*2个牌数组
		 * @default
		 */
		private var arrShowCar:Array;
		private var arrPlayerDeal:Array;
		/**
		 * 手中的两只牌
		 * @default
		 */
		private var _arrMyCard:Array;
		private var timer:Timer;
		private var timerTwo:Timer;
		private var _timerCont:Timer;
		private var deal:int=0;
		private var len:int=0;
		private var _lostArr:Array;
		private var _winArr:Array;
		private var _arrWinCard:Array;
		private var _global:GameData;
		private var _inRoomData:InRoomPlayerDataProxy;
		public static const NAME:String="DealCardMediator";

		public function DealCardMediator(viewComponent:Object=null)
		{
			this.arrShowCar=new Array();
			this.arrPlayerDeal=new Array();
			this._arrMyCard=[];
			this._global=GameData.getInstance();
			super(NAME, viewComponent);
			this._inRoomData=facade.retrieveProxy(InRoomPlayerDataProxy.NAME) as InRoomPlayerDataProxy;
			return;
		}

		private function init():void
		{
			var cardMgr:CardManage=null;
			this.arrDeal=[[this.mcDeal.card1_1, this.mcDeal.card2_1], [this.mcDeal.card1_2, this.mcDeal.card2_2], [this.mcDeal.card1_3, this.mcDeal.card2_3], [this.mcDeal.card1_4, this.mcDeal.card2_4], [this.mcDeal.card1_5, this.mcDeal.card2_5], [this.mcDeal.card1_6, this.mcDeal.card2_6], [this.mcDeal.card1_7, this.mcDeal.card2_7], [this.mcDeal.card1_8, this.mcDeal.card2_8], [this.mcDeal.card1_9, this.mcDeal.card2_9]];
			this.mcDeal.cardback.visible=false;
			var count:int=0;
			while (count < this.arrDeal.length)
			{

				this.arrDeal[count][0].visible=false;
				this.arrDeal[count][1].visible=false;
				count=count + 1;
			}
			count=0;
			while (count < 18)
			{

				cardMgr=new CardManage(GameData.getMovieClip("Card"));
				cardMgr.id=count;
				this.arrShowCar.push(cardMgr);
				count=count + 1;
			}
			return;
		}

		private function initTimer():void
		{
			this.timer=new Timer(220, this.len);
			this.timer.addEventListener(TimerEvent.TIMER, this.timerHandler);
			this.timer.addEventListener(TimerEvent.TIMER_COMPLETE, this.timerCompleteHandler);
			this.timerTwo=new Timer(220, this.len);
			this.timerTwo.addEventListener(TimerEvent.TIMER, this.timerTwoHandler);
			this.timerTwo.addEventListener(TimerEvent.TIMER_COMPLETE, this.timerTwoCompleteHandler);
			this.timer.start();
			return;
		}

		private function timerTwoHandler(event:TimerEvent):void
		{
			var cardMgr:CardManage=this.arrShowCar[int(this.arrPlayerDeal[this.deal]) + 9 - 1] as CardManage;
			if (cardMgr == null)
				return;
			cardMgr.card.gotoAndStop(1);
			cardMgr.visible=true;
			this.mcDeal.addChild(cardMgr);
			cardMgr.x=this.mcDeal.cardback.x;
			cardMgr.y=this.mcDeal.cardback.y;
			cardMgr.rotation=this.mcDeal.cardback.rotation;
			var point:*=new Point(this.arrDeal[(this.arrPlayerDeal[this.deal] - 1)][1].x, this.arrDeal[(this.arrPlayerDeal[this.deal] - 1)][1].y);
			var _rotation:*=this.arrDeal[(this.arrPlayerDeal[this.deal] - 1)][1].rotation;
			var _scaleX:*=this.arrDeal[(this.arrPlayerDeal[this.deal] - 1)][0].scaleX;
			var _scaleY:*=this.arrDeal[(this.arrPlayerDeal[this.deal] - 1)][0].scaleY;
			TweenMax.to(cardMgr, 0.3, {x: point.x, y: point.y, scaleX: _scaleX, scaleY: _scaleY, rotation: _rotation, onComplete: this.onFinishTweenTwo, onCompleteParams: [this.arrPlayerDeal[this.deal]]});
			SoundManage.playSound("Deal");
			this.deal++;
			return;
		}

		private function timerTwoCompleteHandler(event:TimerEvent):void
		{
			this.timerTwo.removeEventListener(TimerEvent.TIMER, this.timerTwoHandler);
			this.timerTwo.removeEventListener(TimerEvent.TIMER_COMPLETE, this.timerTwoCompleteHandler);
			return;
		}

		private function timerHandler(event:TimerEvent):void
		{
//			if (this.deal < 1 || this.deal > 9)
//			{
//				trace("DealCardMediator.timerHandler this.deal invalid ");
//				return;
//			}
			var cardMgr:CardManage=this.arrShowCar[(int(this.arrPlayerDeal[this.deal]) - 1)] as CardManage;
			if (cardMgr == null)
			{
				return;
//				trace((int(this.arrPlayerDeal[this.deal]) - 1) + " cardMgr null");
//				return;
			}
			cardMgr.card.gotoAndStop(1);
			cardMgr.visible=true;
			this.mcDeal.addChild(cardMgr);
			cardMgr.x=this.mcDeal.cardback.x;
			cardMgr.y=this.mcDeal.cardback.y;
			cardMgr.rotation=this.mcDeal.cardback.rotation;
			var point:*=new Point(this.arrDeal[(this.arrPlayerDeal[this.deal] - 1)][0].x, this.arrDeal[(this.arrPlayerDeal[this.deal] - 1)][0].y);
			var rotation:*=this.arrDeal[(this.arrPlayerDeal[this.deal] - 1)][0].rotation;
			var _scaleX:*=this.arrDeal[(this.arrPlayerDeal[this.deal] - 1)][0].scaleX;
			var _scaleY:*=this.arrDeal[(this.arrPlayerDeal[this.deal] - 1)][0].scaleY;
			TweenMax.to(cardMgr, 0.3, {x: point.x, y: point.y, scaleX: _scaleX, scaleY: _scaleY, rotation: rotation, onComplete: this.onFinishTweenOne, onCompleteParams: [this.arrPlayerDeal[this.deal]]});
			SoundManage.playSound("Deal");
			this.deal++;
			return;
		}

		private function timerCompleteHandler(event:TimerEvent):void
		{
			this.deal=0;
			this.timer.removeEventListener(TimerEvent.TIMER, this.timerHandler);
			this.timer.removeEventListener(TimerEvent.TIMER_COMPLETE, this.timerCompleteHandler);
			this.timerTwo.start();
			return;
		}

		private function onFinishTweenOne(seatId:int):void
		{
			var playerVo:*=this._inRoomData.getSitPlayerBySeatId(seatId);
			playerVo.state=GameData.PLAYING_GAME;
			if (this._global.seatId == seatId)
			{
				this.showMyCardOne(seatId, this._arrMyCard[0]);
			}
			return;
		}

		private function onFinishTweenTwo(seatId:int):void
		{
			var msg:String=null;
			if (this._global.seatId == seatId)
			{
				//%NAME% 牌型为%VALUE%
				this.showMyCardTwo(seatId, this._arrMyCard[1]);
				msg=GameData.replaceStrValue(GameData.tip["MSG_PLAYER_CARD"], "VALUE", GameData.getCardIcon(this._arrMyCard[0]) + "," + GameData.getCardIcon(this._arrMyCard[1]));
				this.sendNotification(CLZNoticeName.CHAT_SYSTEM_TIP, msg);
			}
			return;
		}

		private function startDeal(dealer:int, arrSit:Array):void
		{
			var _loc_3:int=0;
			var _loc_4:*=undefined;
			var _loc_5:int=0;
			this.deal=0;
			this.arrPlayerDeal=arrSit;
			this.len=this.arrPlayerDeal.length;
			for (_loc_4 in this.arrPlayerDeal)
			{

				if (this.arrPlayerDeal[_loc_4] == dealer)
				{
					_loc_3=_loc_4 + 1;
				}
			}
			_loc_5=0;
			while (_loc_5 < _loc_3)
			{

				this.arrPlayerDeal.push(this.arrPlayerDeal.shift());
				_loc_5=_loc_5 + 1;
			}
			this.initTimer();
			return;
		}

		private function showSitCard(seatId:int):void
		{
			var cardMgr:*=this.arrShowCar[(seatId - 1)] as CardManage;
			cardMgr.card.gotoAndStop(1);
			var cardMgr2:*=this.arrShowCar[(seatId - 1) + 9] as CardManage;
			cardMgr2.card.gotoAndStop(1);
			cardMgr.visible=true;
			cardMgr2.visible=true;
			this.mcDeal.addChild(cardMgr);
			cardMgr.x=this.arrDeal[(seatId - 1)][0].x;
			cardMgr.y=this.arrDeal[(seatId - 1)][0].y;
			cardMgr.rotation=this.arrDeal[(seatId - 1)][0].rotation;
			cardMgr.scaleX=this.arrDeal[(seatId - 1)][0].scaleX;
			cardMgr.scaleY=this.arrDeal[(seatId - 1)][0].scaleY;
			this.mcDeal.addChild(cardMgr2);
			cardMgr2.x=this.arrDeal[(seatId - 1)][1].x;
			cardMgr2.y=this.arrDeal[(seatId - 1)][1].y;
			cardMgr2.rotation=this.arrDeal[(seatId - 1)][1].rotation;
			cardMgr2.scaleX=this.arrDeal[(seatId - 1)][1].scaleX;
			cardMgr2.scaleY=this.arrDeal[(seatId - 1)][1].scaleY;
			return;
		}

		private function removeCard(seatId:int):void
		{
			if (this.arrShowCar[(seatId - 1)] != null)
			{
				if (this.mcDeal.contains(this.arrShowCar[(seatId - 1)]))
				{
					this.arrShowCar[(seatId - 1)].removeThis();
					this.mcDeal.removeChild(this.arrShowCar[(seatId - 1)]);
				}
			}

			if (this.arrShowCar[(seatId - 1) + 9] != null)
			{
				if (this.mcDeal.contains(this.arrShowCar[(seatId - 1) + 9]))
				{
					this.arrShowCar[(seatId - 1) + 9].removeThis();
					this.mcDeal.removeChild(this.arrShowCar[(seatId - 1) + 9]);
				}
			}

			return;
		}

		private function removeAllCard():void
		{
			var totalCard:*=this.arrShowCar.length;
			var index:int=0;
			while (index < totalCard)
			{

				if (this.arrShowCar[index] != null)
				{
					this.arrShowCar[index].removeThis();
					if (this.mcDeal.contains(this.arrShowCar[index]))
					{
						this.mcDeal.removeChild(this.arrShowCar[index]);
					}
					TweenMax.to(this.arrShowCar[index], 0, {colorMatrixFilter: {}});
					this.arrShowCar[index]=null;
				}
				index=index + 1;
			}
			return;
		}

		private function showMyCardOne(seatId:int, cardStr:String):void
		{
			if (this.arrShowCar == [])
			{
				return;
			}
			if (cardStr != "--" && this.arrShowCar[(seatId - 1)] != null)
			{
				this.arrShowCar[(seatId - 1)].showCard(cardStr);
				SoundManage.playSound("CardFlip");
			}

			return;
		}

		private function showMyCardTwo(seatId:int, cardStr:String):void
		{
			if (cardStr == "-")
			{
				return;
			}
			if (this.arrShowCar == null || this.arrShowCar.length == 0)
			{
				return;
			}
			this.arrShowCar[(seatId - 1) + 9].showCard(cardStr);
			SoundManage.playSound("CardFlip");
			return;
		}

		private function showAllPlayerCard(arrLostPlayer:Array, arrWinPlayer:Array, param3:Array):void
		{
			this._lostArr=arrLostPlayer;
			this._winArr=arrWinPlayer;
			this._arrWinCard=param3;
			if (this._timerCont != null)
			{
				this._timerCont.removeEventListener(TimerEvent.TIMER, this.timerContItemHandler);
				this._timerCont=null;
			}
			this._timerCont=new Timer(500, 4);
			this._timerCont.addEventListener(TimerEvent.TIMER, this.timerContItemHandler);
			this._timerCont.reset();
			this._timerCont.start();
			return;
		}

		private function timerContItemHandler(event:TimerEvent):void
		{
			switch (this._timerCont.currentCount)
			{
				case 1:
				{
					this.showLostCards(this._lostArr);
					break;
				}
				case 2:
				{
					this.hideLostCards(this._lostArr);
					break;
				}
				case 3:
				{
					this.showWinCards(this._winArr);
					break;
				}
				case 4:
				{
					this.sendNotification(CLZNoticeName.DRAW_GAME);
					this.showPokerTypeTip(this._winArr);
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		}

		private function showPokerTypeTip(param1:Array):void
		{
			return;
		}

		private function showLostCards(arrLostPlayer:Array):void
		{
			var lostPlayer:Object=null;
			var index:int=0;
			while (index < arrLostPlayer.length)
			{

				lostPlayer=arrLostPlayer[index];
				this.showMyCardOne(lostPlayer.seatId, lostPlayer.card1);
				this.showMyCardTwo(lostPlayer.seatId, lostPlayer.card2);
				index=index + 1;
			}
			return;
		}

		private function hideLostCards(arrLostPlayer:Array):void
		{
			var lostPlayer:Object=null;
			var index:int=0;
			while (index < arrLostPlayer.length)
			{
				lostPlayer=arrLostPlayer[index];
				if (this.arrShowCar[(lostPlayer.seatId - 1) + 9] != null)
				{
					TweenMax.to(this.arrShowCar[(lostPlayer.seatId - 1) + 9], 0, {colorMatrixFilter: {colorize: 0, amount: 0.6}});
				}
				if (this.arrShowCar[(lostPlayer.seatId - 1)] != null)
				{
					TweenMax.to(this.arrShowCar[(lostPlayer.seatId - 1)], 0, {colorMatrixFilter: {colorize: 0, amount: 0.6}});
				}
				index=index + 1;
			}
			return;
		}

		private function showWinCards(arrWin:Array):void
		{
			var winplayer:Object=null;
			var index:int=0;
			while (index < arrWin.length)
			{

				winplayer=arrWin[index];
				this.showMyCardOne(winplayer.seatId, winplayer.card1);
				this.showMyCardTwo(winplayer.seatId, winplayer.card2);
				index=index + 1;
			}
			return;
		}

		/**
		 * 给坐位上5张牌加阴影特效,选中
		 * @param seatId
		 * @param arrWinPlayer5Card
		 * @param arrWinPlayer2Card
		 */
		private function selectedCard(seatId:int, arrWinPlayer5Card:Array, arrWinPlayer2Card:Array):void
		{
			//脚本超时
			if (arrWinPlayer5Card[0] == "-")
				return;
			var _playerVo:int=0;
			var copyArr5Card:*=arrWinPlayer5Card.concat();
			var index:int=0;
			while (index < this.arrDeal.length)
			{
				try
				{
					if(this.arrShowCar[index]!=null&&this.arrShowCar[index + 9]!=null){
						TweenMax.to(this.arrShowCar[index + 9], 0, {colorMatrixFilter: {colorize: 0, amount: 0.6}});
						TweenMax.to(this.arrShowCar[index], 0, {colorMatrixFilter: {colorize: 0, amount: 0.6}});
					}
				}
				catch (error:Error)
				{
				}
				index=index + 1;

			}
			var arrIndex:Array=[];
			var index2:int=0;
			while (index2 < copyArr5Card.length)
			{

				_playerVo=0;
				while (_playerVo < arrWinPlayer2Card.length)
				{

					if (arrWinPlayer2Card[_playerVo] == copyArr5Card[index2])
					{
						arrIndex.push(_playerVo);
					}
					_playerVo=_playerVo + 1;
				}
				index2=index2 + 1;
			}
			index2=0;
			while (index2 < arrIndex.length)
			{

				if (arrIndex[index2] == 0)
				{
					TweenMax.to(this.arrShowCar[(seatId - 1)], 0, {colorMatrixFilter: {}});
				}
				if (arrIndex[index2] == 1)
				{
					TweenMax.to(this.arrShowCar[(seatId - 1) + 9], 0, {colorMatrixFilter: {}});
				}
				index2=index2 + 1;
			}
			return;
		}

		override public function onRegister():void
		{
			this.init();
			return;
		}

		override public function listNotificationInterests():Array
		{
			return [CLZNoticeName.START_GAME, CLZNoticeName.SHOW, CLZNoticeName.GAME_OVER, CLZNoticeName.STAND_UP_RSP, CLZNoticeName.POTS, CLZNoticeName.SITDOWN_RSP, CLZNoticeName.SHOW_MY_CARD_AGAIN, CLZNoticeName.OPERATION_SUCC, CLZNoticeName.OUT_GAME_ROOM_REMOVE];
		}

		override public function handleNotification(notification:INotification):void
		{
			var data:Object=null;
			var msg:String=null;
			var allPlayerLen:int=0;
			var arrLostPlayer:Array=null;
			var _loc_6:Array=null;
			var arrWinPlayer:Array=null;
			var _playerVo:PlayerVo=null;
			var i:int=0;
			var cardMgr:CardManage=null;
			data=notification.getBody();
			switch (notification.getName())
			{
				case CLZNoticeName.START_GAME:
				{
					this.sendNotification(CLZNoticeName.MOVE_DEALER, data.dealer);
					this._arrMyCard=[data.card1, data.card2];
					i=0;
					while (i < 18)
					{

						cardMgr=new CardManage(GameData.getMovieClip("Card"));
						cardMgr.id=i;
						this.arrShowCar[i]=cardMgr;
						i=i + 1;
					}
					msg=GameData.tip["MSG_START_NEW_GAME"]; //新牌局开始
					this.sendNotification(CLZNoticeName.CHAT_SYSTEM_TIP, msg);
					this.startDeal(data.dealer, data.arrSit);
					break;
				}
				case CLZNoticeName.SHOW:
				{
					data=notification.getBody();
					allPlayerLen=data.arrPlayer.length;
					arrLostPlayer=[];
					arrWinPlayer=[];
					i=0;
					while (i < allPlayerLen)
					{

						if (data.arrPlayer[i].isWin)
						{
							arrWinPlayer.push(data.arrPlayer[i]);
						}
						else
						{
							arrLostPlayer.push(data.arrPlayer[i]);
						}
						i=i + 1;
					}
					this.showAllPlayerCard(arrLostPlayer, arrWinPlayer, []);
					break;
				}
				case CLZNoticeName.POTS:
				{
					arrWinPlayer=data.arrWinPlayer;
					this.selectedCard(arrWinPlayer[0].seatId, [arrWinPlayer[0].card1, arrWinPlayer[0].card2, arrWinPlayer[0].card3, arrWinPlayer[0].card4, arrWinPlayer[0].card5], [arrWinPlayer[0].backCard1, arrWinPlayer[0].backCard2]);
					break;
				}
				case CLZNoticeName.GAME_OVER:
				{
					this.initCar();
					break;
				}
				case CLZNoticeName.STAND_UP_RSP:
				{
					this.removeCard(data.seatId);
					break;
				}
				case CLZNoticeName.SITDOWN_RSP:
				{
					_playerVo=this._inRoomData.getInRoomPlayer(data.userid) as PlayerVo;
					if (_playerVo.state != GameData.PLAYING_GAME) //如果玩家掉线，将之前手牌显示
					{
						if (_playerVo.state == GameData.PLAYING_CLOSE_SOCKE)
						{
							this.showSitCard(_playerVo.seatId);
						}
					}
					break;
				}
				case CLZNoticeName.SHOW_MY_CARD_AGAIN:
				{
					this._arrMyCard=data as Array;
					this.showMyCardOne(this._global.seatId, this._arrMyCard[0]);
					this.showMyCardTwo(this._global.seatId, this._arrMyCard[1]);
					break;
				}
				case CLZNoticeName.OPERATION_SUCC:
				{
					//监视放弃，给牌加阴影特效
					if (data.lastAction == GameData.FOLD)
					{
						TweenMax.to(this.arrShowCar[(data.seatId - 1) + 9], 0, {colorMatrixFilter: {colorize: 0, amount: 0.6}});
						TweenMax.to(this.arrShowCar[(data.seatId - 1)], 0, {colorMatrixFilter: {colorize: 0, amount: 0.6}});
					}
					break;
				}
				case CLZNoticeName.OUT_GAME_ROOM_REMOVE:
				{
					this.removeThis();
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		}

		private function removeThis():void
		{
			if (this.timer != null)
			{
				this.timer.reset();
				this.timer.removeEventListener(TimerEvent.TIMER, this.timerHandler);
				this.timer.removeEventListener(TimerEvent.TIMER_COMPLETE, this.timerCompleteHandler);
				this.timer=null;
			}
			if (this.timerTwo != null)
			{
				this.timerTwo.reset();
				this.timerTwo.removeEventListener(TimerEvent.TIMER, this.timerTwoHandler);
				this.timerTwo.removeEventListener(TimerEvent.TIMER_COMPLETE, this.timerTwoCompleteHandler);
				this.timerTwo=null;
			}
			if (this._timerCont != null)
			{
				this._timerCont.reset();
				this._timerCont.removeEventListener(TimerEvent.TIMER, this.timerContItemHandler);
				this._timerCont=null;
			}
			this.removeAllCard();
			this.arrShowCar=[];
			this._arrMyCard=[];
			GameData.removeNumChildre(this.mcDeal, true);
			return;
		}

		private function initCar():void
		{
			this._arrMyCard=[];
			this.removeAllCard();
			this.arrShowCar=[];
			return;
		}

		private function get mcDeal():MovieClip
		{
			return viewComponent as MovieClip;
		}

	}
}
